package org.prail.net.core;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import org.prail.net.Configuration;
import org.prail.net.Web;

import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.NameValuePair;


/**
 * 
 * @author Parnassian
 *
 */
public class Login {


	private String username;
	private String password;

	/**
	 * Logs in on vk.com
	 * @param username
	 * @param password
	 * @throws IOException
	 */
	public Login(final String username, final String password) {
		this.username = username;
		this.password = password;
    }
	
	public boolean login() throws IOException {
		final WebClient wb = Web.getClient();
        wb.setJavaScriptEnabled(true);
        wb.setCssEnabled(false);
        wb.setActiveXNative(false);
        wb.setAppletEnabled(false);
        wb.setThrowExceptionOnFailingStatusCode(false);
        
        final WebRequest requestSettings = new WebRequest(new URL(Configuration.loginHost), HttpMethod.POST);
        requestSettings.setRequestParameters(new ArrayList<NameValuePair>());
        requestSettings.getRequestParameters().add(new NameValuePair("email", username));
        requestSettings.getRequestParameters().add(new NameValuePair("pass", password));
        
        HtmlPage check = wb.getPage(requestSettings);
        return !check.asText().contains("Login failed");
	}
}
